package com.kk.xhr.model.dao.custom;

import com.kk.xhr.model.dto.DepartmentTreeDTO;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

/**
 * 自定义部门mapper
 *
 * @author kkmystery
 * @version 1.0 2021/3/4
 * @since 1.0.0
 */
@Mapper
public interface DepartmentCustomMapper {

    /**
     * 根据id查询部门以及子部门
     *
     * @param id 主键id
     * @return DepartmentTreeDTO
     */
    @Select("select * from department where parent_id = #{id} and status = 1")
    @Results(id = "departmentTreeMap", value = {
        @Result(id = true, property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "parentId", column = "parent_id"),
        @Result(property = "path", column = "path"),
        @Result(property = "parent", column = "parent"),
        @Result(property = "children", column = "id",
            many = @Many(select = "com.kk.xhr.model.dao.custom.DepartmentCustomMapper.query",
                fetchType = FetchType.EAGER)),
    })
    DepartmentTreeDTO query(@Param("id") Long id);
}
